# ブックマーク削除APIクラス

require "rb/api.rb"
require "csv"

$KCODE = "UTF"

class DeleteAPI < API

    def initialize
        super
        print result_by_jsonp
    end

    def result_by_jsonp
        if check_auth then
            delete
            attach_callback "{result: true}"
        else
            attach_callback "{result: false}"
        end
    end

    def delete
        bookmarks = get_raw_bookmarks().delete_if {|i| i[0] == @cgi["url"]}
        writer = CSV.open(@bookmarks_file, 'w')
        bookmarks.each do |i|
            writer << i
        end
        writer.close
    end

    def attach_callback(json_str)
        return 'Obm.deleteAPICallBack(' + json_str + ');'
    end
end

